<template>
  <Dialog :title="title" :model-value="visible" width="600px" :before-close="closeForm">
    <Form
      :schema="schema"
      :model="flow"
      @register="formRegister"
      :isCol="false"
      label-width="120px"
      :rules="rules"
    />
    <template #footer>
      <el-space>
        <BaseButton :loading="loading" type="primary" @click="save">确定</BaseButton>
        <BaseButton @click="resetForm">重置</BaseButton>
      </el-space>
    </template>
  </Dialog>
</template>

<script>
import { DictItemDic } from '@/flow/config/flowConfig'
import * as flowApi from '@/flow/api/flow'
import { Dialog } from '@/components/Dialog'
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
const { formRegister, formMethods } = useForm()
export default {
  components: {
    Dialog,
    Form
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: null
    },
    flowType: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      DictItemDic,
      title: '新增流程',
      source: {},
      schema: [
        {
          field: 'FlowName',
          label: '流程名',
          component: 'Input',
          componentProps: {
            maxLength: 50,
            placeholder: '流程名'
          }
        },
        {
          field: 'FlowType',
          label: '流程分类',
          component: 'TreeDicItem',
          componentProps: {
            placeholder: '流程分类',
            dicId: DictItemDic.flowType
          }
        },
        {
          field: 'DefFormId',
          label: '默认表单',
          component: 'FormInput',
          componentProps: {
            placeholder: '选择填报的表单',
            prtFormClassify: '02',
            formType: [0, 1]
          }
        },
        {
          field: 'Remark',
          label: '备注',
          component: 'Input',
          componentProps: {
            maxLength: 255,
            type: 'textarea',
            placeholder: '流程名'
          }
        }
      ],
      flow: {
        FlowName: null,
        FlowType: null,
        Remark: null
      },
      rules: {
        FlowName: [
          {
            required: true,
            message: '流程名不能为空！',
            trigger: 'blur'
          }
        ],
        FlowType: [
          {
            required: true,
            message: '流程分类不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  methods: {
    formRegister,
    async reset() {
      if (this.id == null) {
        this.title = '新增流程'
        this.source = null
        this.flow = {
          FlowName: null,
          FlowType: this.flowType,
          Remark: null,
          DefFormId: null
        }
      } else {
        const data = await flowApi.Get(this.id)
        this.source = data
        this.flow = Object.assign({}, data)
        this.title = '编辑流程:' + data.FlowName
      }
      formMethods.setValues(this.flow)
    },
    async save() {
      const data = await formMethods.validate()
      if (data == null) {
        return
      } else if (this.id) {
        this.set(data)
      } else {
        this.add(data)
      }
    },
    async set(data) {
      await flowApi.Set(this.id, data)
      this.$message({
        message: '更新成功!',
        type: 'success'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true)
    },
    async add(data) {
      await flowApi.Add(data)
      this.$message({
        message: '添加成功!',
        type: 'success'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true)
    },
    closeForm() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    },
    resetForm() {
      if (this.id == null) {
        this.flow = {
          FlowName: null,
          FlowType: this.flowType,
          Remark: null,
          DefFormId: null
        }
      } else {
        this.flow = Object.assign({}, this.source)
      }
      formMethods.setValues(this.flow)
    }
  }
}
</script>
